package LearnAlgorithm.h_标准数学公式;


/*
int[] arr = new int[] {1^2, 2^2, 3^2, 4^2, ...}连续自然数
int[] arr = new int[] {1^3, 2^3, 3^3, 4^3, ...}连续自然数
 */
public class c平方和and立方和 {
	public static void main(String[] args) {
		int[] arr2 = new int[] {1*1,2*2,3*3,4*4};//30
		int[] arr3 = new int[] {1*1*1,2*2*2,3*3*3,4*4*4};//100
		System.out.println(sumSquare(arr2));
		System.out.println(sumCube(arr3));
	}
	
	/**
	 * 连续自然数平方和
	 * d=1
	 * a1=1
	 * {a1^2, (a1+1)^2, ((a1+1)+1)^2, ...}
	 * @return
	 */
	public static int sumSquare(int[] arr) {
		int n = arr.length;
		return (n * (n + 1) * (2 * n + 1)) / 6;
	}
	
	/**
	 * 连续自然数立方和
	 * d=1
	 * a1=1
	 * {a1^3, (a1+1)^3, ((a1+1)+1)^3, ...}
	 * @return
	 */
	public static int sumCube(int[] arr) {
		int n = arr.length;
		return (n * n * (n + 1) * (n + 1)) / 4;
	}
}
